package com.wd.health.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName:shopmall
 * @Package:com.bawei.myhealth.utils
 * @ClassName:SQLiteDao
 * @UpdateUser: 裴童童
 * @date: 2020/12/11
 */
public class SQLiteDao {
    private PersonSQLiteOpenHelper sqLiteOpenHelper;

    public SQLiteDao(Context context) {
        sqLiteOpenHelper = new PersonSQLiteOpenHelper(context);
    }

    public long add(String name, String number) {
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
//      db.execSQL("insert  into person (name,number) values (?,?)", new String[]{name, number});
        ContentValues values=new ContentValues();
        values.put("name",name);
        values.put("number",number);
        //id返回的是数据的id，如果返回-1时表示未添加成功
        long id=db.insert("person",null,values);
        db.close();
        return id;
    }

    /**
     * 查找一条数据
     * @param name 根据名字
     * @return true 找到了 false没找到
     */
    public boolean find(String name) {
        SQLiteDatabase db = sqLiteOpenHelper.getReadableDatabase();
//      Cursor cursor = db.rawQuery("select * from person where name=?", new String[]{name});
        Cursor cursor=db.query("person",null,"name=?",new String[]{name},null,null,null);
        boolean b = cursor.moveToNext();
        cursor.close();
        db.close();
        return b;
    }

    /**
     * 修改一条数据
     * @param newName 修改名字
     * @param number 根据电话
     */
    public void update(String newName,String number){
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
//        db.execSQL("update person set name=? where number=?", new String[]{name, number});
        ContentValues values=new ContentValues();
        values.put("name",newName);
        db.update("person",values,"number=?",new String[]{number});
        db.close();
    }

    /**
     * 删除一条数据
     */
    public int  delete(int id){
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
//        db.execSQL("delete from person where name=?",new String[]{name});
        int i=db.delete("person","id=?",new String[]{String.valueOf(id)});
        db.close();
        //i返回的是删除的是第几行的数据，返回0表示删除不成功
        return i;
    }


    /**
     * 查找数据库中所有的数据
     * @return 返回persons的lsit
     */
    public List<Person> findAll(){
        List<Person> persons=new ArrayList();
        SQLiteDatabase db=sqLiteOpenHelper.getReadableDatabase();
//        Cursor cursor=db.rawQuery("select *from person",null);
        Cursor cursor=db.query("person",new String[]{"id","name","number"},null,null,null,null,null);
        while(cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            String number=cursor.getString(cursor.getColumnIndex("number"));
            Person person=new Person(id,name,number);
            persons.add(person);
        }
        cursor.close();
        db.close();
        return persons;
    }
} 
